# -*- coding: UTF-8 -*-

import re
from enum import Enum
from Sample import *
import os

class SampleMode(Enum):
    One=0

class SampleText:
    def __init__(self, mode: SampleMode):
        self.config = Sample[mode.value]

        # 存储文章
        self.summary  = ''
        self.contents = {}
        self.file_data = self.readSampleFile(self.config['html'])

    # 读取模版文件
    def readSampleFile(self, fileName: str):
        current_file_path = os.path.abspath(__file__)
        current_directory = os.path.dirname(current_file_path)
        article_template_file_path = os.path.join(current_directory, '模版', fileName)
        
        with open(article_template_file_path, 'r', encoding='utf-8') as f:
            return f.read()

    # 添加：摘要
    def addSummary(self, text: str):
        stype = self.config['summary']['type']
        if stype == "pattern" :
            self.file_data = self.addPattern(pattern=self.config['summary']['pattern'], replace=self.config['summary']['replace'], text=text)

    ##### Section #####
    def addTitle(self, section: int, title: str):
        content_info = {}
        if section in self.contents:
            content_info = self.contents[section]

        content_info['title'] = title
        self.contents[section] = content_info

    # 正则表达式
    def addPattern(self, pattern: str, replace: str, text: str):
        return re.sub(pattern=pattern, repl=replace.format(text), string=self.file_data, flags=re.DOTALL)
    
    # 顺序添加
    def addSort(self, start: str, text: str, end: str, content: str):
        return start + text.replace('#######', content) + end

    def addSectionText(self, section: int, text: str):
        content_info = {}
        if section in self.contents:
            content_info = self.contents[section]
        
        texts = [] if  not 'contents' in content_info else content_info['contents']
        texts.append({'type' : 'text', 'text' : text})
        content_info['contents'] = texts
        self.contents[section] = content_info

    def addSectionImage(self, section: int, image: str):
        content_info = {}
        if section in self.contents:
            content_info = self.contents[section]
        
        texts = [] if  not 'contents' in content_info else content_info['contents']
        texts.append({'type' : 'image', 'text' : image})
        content_info['contents'] = texts
        self.contents[section] = content_info

    def getHtmlTitle(self, title: str):
        ttype = self.config['section']['title']['type']
        if ttype == "sort":
            return self.addSort(start=self.config['section']['title']['start'], text=self.config['section']['title']['text'], end=self.config['section']['title']['end'], content=title)
        else:
            return title


    def getHtmlContent(self, textInfos: list[dict]):
        content = self.config['section']['content']['start']
        for info in textInfos:
            if info['type'] == 'text':
                content += self.config['section']['content']['text'].replace('########', info['text'])
            elif info['type'] == 'image':
                content += self.config['section']['content']['image'].replace('########', info['text'])

        return content + self.config['section']['content']['end']

    def save(self):
        print(self.contents)
        content = ""
        for section in sorted(self.contents.keys()):
            content_info = self.contents[section]

            # 添加：标题
            content += self.getHtmlTitle(content_info['title'])

            # 添加：内容
            content += self.getHtmlContent(content_info['contents'])

            
            # 添加：换行
            content += self.config['line']

        print(content)

        self.file_data = self.addPattern(pattern=self.config['content']['pattern'], replace=self.config['content']['replace'], text=content)    

        current_file_path = os.path.abspath(__file__)
        current_directory = os.path.dirname(current_file_path)
        article_template_file_path = os.path.join(current_directory, '模版', self.config['html'])
        with open(article_template_file_path, 'w', encoding='utf-8') as f:
            f.write(self.file_data)
        

if __name__ == '__main__':
    sample =  SampleText(SampleMode.One)

    sample.addSummary(text="今天的天气很好今天的天气很好今天的天气很好今天的天气很好今天的天气很好今天的天气很好今天的天气很好今天的天气很好今天的天气很好")

    sample.addTitle(1, "气很好今天的天气很好今天的天气很好今天")
    sample.addTitle(0, "2222")
    
    sample.addSectionText(0, "房东司法鉴定所房东舒服撒放")
    sample.addSectionText(0, "房东舒服很多房东说废话都快撒范德萨范德萨房东舒服很多房东说废话都快撒范德萨范德萨房东舒服很多房东说废话都快撒范德萨范德萨")

    sample.addSectionText(1, "11111房东fds fdsa fdsa服很多房东说废话都快撒范德萨范德萨房东舒服很多房东说废话都快撒范德萨范德萨房东舒服很多房东说废话都快撒范德萨范德萨服撒放")
    sample.addSectionText(1, "11111房东舒服很多房东说废话都快撒范德萨房东舒服很多房东说废话都快撒范德萨范德萨房东舒服很多房东说废话都快撒范德萨范德萨范德萨1111")

    sample.save()